package org.wzq;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.wzq.bean.Course;
import org.wzq.bean.Student;

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();

        Student lilei = new Student("lilei");
        Student hmm = new Student("hmm");

        Course math = new Course("math");
        Course history = new Course("history");
        Course english = new Course("english");

        Set<Course> courseSet1 = new HashSet<Course>(11);
        courseSet1.add(math);
        courseSet1.add(english);
        lilei.setCourseSet(courseSet1);

        Set<Course> courseSet2 = new HashSet<Course>(11);
        courseSet2.add(history);
        courseSet2.add(english);
        hmm.setCourseSet(courseSet2);

        // save
        Transaction tx = session.beginTransaction();
        session.save(lilei);
        session.save(hmm);
        tx.commit();

        // at last
        session.close();
    }
}
